import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd from 'ant-design-vue'
import {message} from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
// import 'ant-design-vue/dist/antd.less';
import axios from 'axios'
import cookies from "vue-cookies";
import * as antIcons from '@ant-design/icons-vue'

const app = createApp(App)

Object.keys(antIcons).forEach(key => {
    app.component(key, antIcons[key])
})
// 添加到全局
app.config.globalProperties.$antIcons = antIcons

app.use(store).use(Antd).use(router).mount('#app')

axios.defaults.withCredentials = true
// axios.defaults.baseURL = 'http://localhost:8088/'
// 上线配置
axios.defaults.baseURL = 'http://47.113.223.105:8088/'

axios.interceptors.request.use(
    (config) => {

        return config
    },
    (error) => {
        Promise.reject(error)
    }
)

axios.interceptors.response.use(
    (response) => {
        //未通过登录认证
        if (response.data.code == 401) {
            //判断cookies是否存在账号密码信息,若存在则登录并更新User信息
            if (cookies.isKey("usercode") && cookies.isKey("password")) {
                axios.post('/user/login', {}).then(rs => {
                    console.log("cookie自动登录结果", rs)
                    if (rs.code == 200) {
                        store.commit('setUser', rs.data)
                        router.push('/home')
                    } else {
                        message.error("当前登录已过期，请重新登陆")
                        router.push("/")
                    }
                    return rs
                })
            } else {
                store.commit('delUser')
                router.push("/")
                message.error(response.data.message)
                return response.data;
            }
        } else {
            return response.data;
        }
    },
    (error) => {
        Promise.reject(error)
    }
)
